<?php

require_once(realpath(dirname(__FILE__)) . '/../config.php');
require_once(realpath(dirname(__FILE__)) . '/../includes/common/ServiceFactory.php');

Session::start();

$config = ServiceFactory::get("SystemConfiguration");
$commonService = ServiceFactory::get();
$userService = ServiceFactory::get("UserService");
$authService = ServiceFactory::get("AuthenticationService");

if(!$authService -> isAuthenticated())
    Response::redirect(Response::addQueryArg(array("redirect_to" => $_SERVER['REQUEST_URI']), "./login.php"));

$user = $authService -> getCurrentUser();

if($authService -> isUser($user))
    die("Access denied!");

$currentFile = 'avatars.php';
$action = $_REQUEST['action'];
$referer = $_REQUEST['referer'];

if (isset($_POST['submit']))
{
    RedirectMessage::removeMessage();
    
    if(isset($_FILES['upload-avatar']) && is_uploaded_file($_FILES['upload-avatar']['tmp_name']))
    {
        try
        {
            $avatarName = $commonService -> uploadAvatar();
            $config -> addNewAvatar(new Avatar($avatarName));
        }
        catch(Exception $ex)
        {
            RedirectMessage::info($ex -> getMessage(), "Error");
        }
        
    }

    Response::redirect($referer);
}
elseif(isset($_POST['deleteit']))
{
    $avatars = $_POST['avatarList'];
    if(!$avatars)
        Response::redirect($referer);
    
    try
    {
        foreach ((array)$avatars as $avatarId)
        {
            $config->removeAvatar($avatarId);
        }
        
        RedirectMessage::info("Avatar đã được xóa");
    }
    catch(Exception $ex)
    {
        RedirectMessage::info($ex -> getMessage(), "Error");
    }
    
    Response::redirect($referer);
}

include("admin-header.php");
include("menu-bar.php");

if(RedirectMessage::hasMessage())
{
    InfoArea::showInfo(RedirectMessage::getMessage(), RedirectMessage::getMessage('title'), RedirectMessage::getMessage('type'));
    RedirectMessage::removeMessage();
}

?>

<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data">
    <input type="hidden" value="<?php echo ($_SERVER['REQUEST_URI']); ?>" name="referer" />
    <div class="ContentInner">
        <div class="TableNav">
            <input type="submit" name="deleteit" class="Button" value="Delete"/>
            <br class="clear"/>
        </div>
        <h2>Danh sách các avatar có sẵn</h2>
        <div class="Entry Manage" id="avatarList">
            <?php nowchat::listAvatar("avatarList"); ?>
        </div>
        <div style="clear:both"></div>
        <h2>Thêm avatar</h2>
        <div class="Entry">
            <?php nowchat::uploadFile("upload-avatar", "Upload avatar", "Chỉ hỗ trợ định dạng JPG, PNG, JPEG, GIF", 35); ?>
        </div>
        <div style="clear:both"></div>
    </div>
    <div class="Command">
        <input type="submit" class="Button" value="Submit" id="submit" name="submit"/>
    </div>
</form>

<?php include("admin-footer.php") ?>
